package com.fann;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LoginServlet() {
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String userid = "";
		String pw1 = "";
		
		if (request.getParameter("userid") != null){
			
			userid = request.getParameter("userid");
			pw1 = request.getParameter("pw1");
			
			Member m = new Member(userid, pw1);
			//Set session
			HttpSession session = request.getSession();
			session.setAttribute("m", m);
			session.setAttribute("isLogin","true");
			
			if (m.loginValidate()){
				//登入成功
			//	request.getRequestDispatcher("success.jsp").forward(request, response);
				response.sendRedirect("loginComplete.jsp");
			} else {
				//登入失敗
				//request.getRequestDispatcher("reg.jsp").forward(request, response);
				response.sendRedirect("login.jsp");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
